<?php

/**
 * 自动生成 MerchantController
 * 
 * Apps/Backend/Controller/MerchantController.class.php
 * Zix @ 2015-06-15
 */

namespace Backend\Controller;

use Common\Service\MerchantService ;

class MerchantController extends BackendController {

  public function index() {
    $action = I('get.action', 'read');

    $data = $this->_init_data();
    $data['module_name'] = '商户';
    $data['module_url'] = U( $this->base_path . 'index');

    $Merchant = MerchantService::instance();
    $data['config'] = array(
      'action' => $action,
      'action_label' => $this->action_label[$action],
      'menu_id' => $this->menu_id ,
      'module_url' => U( $this->base_path . 'index' ),
      'read_url'   => U( $this->base_path . 'read'  ),
      'create_url' => U( $this->base_path . 'create'),
      'update_url' => U( $this->base_path . 'update'),
      'delete_url' => U( $this->base_path . 'delete'),
      'detail_url' => U( $this->base_path . 'detail') . '/',
      'page' => 1,
      'page_size' => C('PAGE_SIZE'),
      'row' => $Merchant->get_default_row() 
    );
    
    $data['area'] = array(
      'get_default_url' => U( $this->module . '/Area/get_default'),
      'get_by_pid_url' => U( $this->module . '/Area/get_by_pid'),
      'get_by_district_url' => U( $this->module . '/Area/get_by_district'),
    );

    $data['param'] = array(
      'status' => $Merchant->get_status() ,
      'type' => $Merchant->get_type() ,
    );

    $this->display_with_layout($data, $this->controller . ':index');
  }

  function read() {
    $post = I('post.');

    $config = array(
      'page' => $post['page'],
      'page_size' => $post['page_size'],
      'status' => $post['status'],
      'type' => $post['type'],
      'keyword' => $post['keyword'],
      'sort' => empty($post['sort']) ? 'id' : $post['sort'],
      'order' => empty($post['order']) ? 'DESC' : $post['order'],
    );

    $Merchant = MerchantService::instance();
    $data['rows'] = $Merchant->get_by_cond($config);

    $config['count'] = TRUE;
    $data['total'] = $Merchant->get_by_cond($config);

    $this->ajaxReturn($data);
  }

  function create() {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }
    
    $Merchant = MerchantService::instance();
    $ret = $Merchant->create($post);
    $this->ajaxReturn($ret);
  }

  function update($id = 0) {
    $post = I('post.');
    if (empty($post)) {
      $this->ajaxReturn(ajax_arr('数据错误,请稍后再试'));
    }

    $Merchant = MerchantService::instance();
    $ret = $Merchant->update($id, $post);
    $this->ajaxReturn($ret);
  }

  function delete() {
    $ids = I('post.ids');
    $Merchant = MerchantService::instance();
    $ret = $Merchant->delete($ids); 
    $this->ajaxReturn($ret);
  }
  
  function detail( $mer_id ) {
    $data = $this->_init_data();
    $data['module_name'] = '商户功能列表';
    $data['module_url'] = U( $this->base_path . 'detail') . '/' . $mer_id ;

    $Merchant = MerchantService::instance();
    
    $data['merchant_data'] = $Merchant->get_by_id( $mer_id ) ;
    
    if ( empty( $data['merchant_data'] ) ) {
      gameover('商户未找到');
    }
    
    $data['config'] = array(
      'menu_id' => 'merchant_data',
      'module_url' => U( $this->base_path . 'index'),
    );

    $this->display_with_layout($data, $this->controller . ':detail'); 
  }
}
